﻿Imports PoliceHealthClass

Public Class frmAddPoliceYearMissing
    Delegate Sub delLoadPoliceYearMissing(ByVal year As String, ByVal organizationId As String)

    Private Sub frmAddPoliceYearMissing_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DBUtilities.BindOrganizatoin(trvOrganization)
        DBUtilities.BindingYear(cbbYear)
    End Sub

    Private Sub trvOrganization_AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles trvOrganization.AfterSelect
        ShowPoliceMissing()
    End Sub

    Private Sub ShowPoliceMissing()
        lblStationName.Text = String.Format("{0}: ", trvOrganization.SelectedNode.Text)
        LoadDataGridView()
        lblPoliceNumber.Text = String.Format("จำนวน {0} คน", dgvPoliceYearMissing.Rows.Count)
    End Sub

    Private Sub LoadDataGridView()
        'ConnectionString.ShowLoadingForm()
        'Me.Invoke(New delLoadPoliceYearMissing(AddressOf LoadPoliceYearMissing), cbbYear.SelectedValue.ToString(), trvOrganization.SelectedNode.Tag.ToString())
        'ConnectionString.TerminateLoadingForm()
        LoadPoliceYearMissing(cbbYear.SelectedValue.ToString(), trvOrganization.SelectedNode.Tag.ToString())
    End Sub

    Private Sub LoadPoliceYearMissing(ByVal year As String, ByVal organizationId As String)
        dgvPoliceYearMissing.Rows.Clear()
        Dim intRowCount As Integer = 0
        Dim policeYearMissings As List(Of Police) = Police.GetPoliceYearMissingByYearAndOrganization(year, organizationId)

        For Each policeYearMissing As Police In policeYearMissings
            dgvPoliceYearMissing.Rows.Add()
            dgvPoliceYearMissing.Rows(intRowCount).Cells("colOrder").Value = intRowCount + 1
            dgvPoliceYearMissing.Rows(intRowCount).Cells("colCitizenId").Value = policeYearMissing.citizen_id
            dgvPoliceYearMissing.Rows(intRowCount).Cells("colFullName").Value = policeYearMissing.prename.prename_short_tha & policeYearMissing.name & " " & policeYearMissing.surname

            intRowCount += 1
        Next
    End Sub

    Private Sub cbbYear_SelectionChangeCommitted(sender As System.Object, e As System.EventArgs) Handles cbbYear.SelectionChangeCommitted
        'ConnectionString.ShowLoadingForm()
        'Me.Invoke(New delLoadPoliceYearMissing(AddressOf LoadPoliceYearMissing), cbbYear.SelectedValue.ToString(), trvOrganization.SelectedNode.Tag.ToString())
        'ConnectionString.TerminateLoadingForm()
        ShowPoliceMissing()
    End Sub

    Private Sub btnFixPoliceYear_Click(sender As System.Object, e As System.EventArgs) Handles btnFixPoliceYear.Click
        If MessageBox.Show("ต้องการเพิ่มเจ้าพนักงานที่หายไปในปี " & cbbYear.SelectedValue.ToString() & " นี้หรือไม่?", "คำถาม", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

            For i As Integer = 0 To dgvPoliceYearMissing.Rows.Count - 1
                Dim police As New Police()
                police.citizen_id = dgvPoliceYearMissing.Rows(i).Cells("colCitizenId").Value.ToString()
                police.organization.organization_id = trvOrganization.SelectedNode.Tag.ToString()

                If Not police.insertPoliceInYearAndOrganization(cbbYear.SelectedValue.ToString(), police) Then
                    MessageBox.Show("ไม่สามารถเพิ่มเจ้าพนักงานได้ ติดต่อผู้ดูแลระบบ", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
            Next

            MessageBox.Show("เพิ่มเจ้าพนักงานเรียบร้อยแล้ว", "ข้อความ", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ShowPoliceMissing()
        End If
    End Sub
End Class